package com.drug.mingkang.controller;


import com.alibaba.druid.util.StringUtils;
import com.drug.mingkang.model.ImmMember;
import com.drug.mingkang.service.ImmMemberService;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (ImmMember)表控制层
 *
 * @author makejava
 * @since 2020-08-12 09:07:54
 */
@RestController
@RequestMapping("immMember")
public class ImmMemberController {
    /**
     * 服务对象
     */
    @Resource
    private ImmMemberService immMemberService;

    @Resource
    private Tools tools;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public ImmMember selectOne(String id) {
        return this.immMemberService.queryById(id);
    }

    /**
     * 查询所有
     * @param immMember
     * @return
     */
    @GetMapping("findAll")
    public Map<String,Object> findAll(ImmMember immMember){

        //查询所有的用户
        List<ImmMember> list = immMemberService.queryAllByLimit(immMember);

        //查询所有的行数
        int count = immMemberService.selectCount(immMember);
        return tools.limit(list, count);

    }


    @PostMapping("addOrUpdate")
    public ResponseMessage addOrUpdate(ImmMember immMember){
        int rows=0;
        List<ImmMember> list = immMemberService.queryAllByLimits(immMember);
        ResponseMessage bm = new ResponseMessage();
        if (list.size()!=0){
            return ResponseMessage.error("账号已存在");
        }else if(StringUtils.isEmpty(immMember.getMemberId())){
            rows =  immMemberService.insert(immMember);
        }else{
            rows =  immMemberService.update(immMember);
        }
        bm.setStatus(rows);
        return bm;
    }


    /**
     * 删除用户
     * @param memberId
     * @return
     */

    @RequestMapping("deleteById")
    @ResponseBody
    public ResponseMessage deleteById(String memberId){
        int rows = immMemberService.deleteById(memberId);

        if(rows > 0){
            return ResponseMessage.success("删除成功");
        }else {
            return ResponseMessage.error("删除失败");

        }
    }

    @RequestMapping("queryAll")
    @ResponseBody
    public List<ImmMember> queryAll(ImmMember immMember){
        return immMemberService.queryAllByLimits(immMember);
    }

    /**
     * WuTao用的查询会员名
     * @param memberAccout
     * @return
     */
    @PostMapping("selectAccout")
    public ImmMember selectAccout(String memberAccout){
        return this.immMemberService.selectAccout(memberAccout);
    }

}